package sketchpad.gui.component;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXNodesList;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import sketchpad.business.function.ComposeService;
import sketchpad.business.function.OperateService;
import sketchpad.business.function.impl.ComposeServiceImpl;
import sketchpad.business.function.impl.OperateServiceImpl;
import sketchpad.gui.control.DrawControl;

import javafx.scene.input.MouseEvent;
import sketchpad.gui.component.event.EventCenter;

public class FuncNodeList {

    private static final String FX_TEXT_FILL_WHITE = "-fx-text-fill:#fff";
    private static final String FX_BACKGROUND_COLOR = "-fx-background-color:#414944";
    private static final String FX_TEXT_FILL_GREY= "-fx-text-fill:#434343";
    private static final String ANIMATED_OPTION_BUTTON = "animated-option-button";
    private static final String ANIMATED_OPTION_SUB_BUTTON = "animated-option-sub-button";
    private static final String ANIMATED_OPTION_SUB_BUTTON2 = "animated-option-sub-button2";

    private DrawControl control;
    private Stage stage;
    private AnchorPane anchorPane;

    private ComposeService composeService;
    private OperateService operateService;

    public FuncNodeList(DrawControl drawControl, Stage stage, AnchorPane pane){
        control=drawControl;
        this.stage=stage;
        anchorPane = pane;
        composeService = new ComposeServiceImpl(control);
        operateService = new OperateServiceImpl(control);
    }

    private JFXNodesList constructPopup(){

        JFXNodesList nodesList3 = new JFXNodesList();
        nodesList3.setSpacing(10);
        // init nodes
        JFXButton nodeList3Button1 = addButton("组合","框选图形组合分解管理",ANIMATED_OPTION_SUB_BUTTON);
        JFXButton nodeList3Button2 = addButton("组合","框选图形组合",ANIMATED_OPTION_SUB_BUTTON2);
        nodeList3Button2.addEventHandler(MouseEvent.MOUSE_CLICKED,event -> {
            composeService.compose();
        });
        JFXButton nodeList3Button3 = addButton("分解","框选图形分解",ANIMATED_OPTION_SUB_BUTTON2);
        nodeList3Button3.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            composeService.decompose();
        });
        nodesList3.addAnimatedNode(nodeList3Button1);
        nodesList3.addAnimatedNode(nodeList3Button2);
        nodesList3.addAnimatedNode(nodeList3Button3);
        nodesList3.setRotate(90);


        JFXNodesList nodesList2 = new JFXNodesList();
        nodesList2.setSpacing(10);
        JFXButton nodeList2Button = addButton("识别","机器通过图像识别技术对框选图形识别",ANIMATED_OPTION_SUB_BUTTON);
        nodeList2Button.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            EventCenter.recognizeEventHandler(stage,control);
        });
        nodesList2.addAnimatedNode(nodeList2Button);

        JFXNodesList nodesList1 = new JFXNodesList();
        nodesList1.setSpacing(10);
        // init nodes
        JFXButton nodeList1Button1 = addButton("鼠标","对选中图形进行操作",ANIMATED_OPTION_SUB_BUTTON);
        JFXButton nodeList1Button3 = addButton("复制","复制",ANIMATED_OPTION_SUB_BUTTON2);
        nodeList1Button3.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            operateService.copy();
        });
        JFXButton nodeList1Button4 = addButton("粘贴","粘贴",ANIMATED_OPTION_SUB_BUTTON2);
        nodeList1Button4.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            operateService.paste();
        });
        JFXButton nodeList1Button5 = addButton("标注","手动编辑图形信息",ANIMATED_OPTION_SUB_BUTTON);
        nodeList1Button5.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            EventCenter.labelEventHandler(stage,control);
        });
        nodesList1.addAnimatedNode(nodeList1Button1);
        nodesList1.addAnimatedNode(nodeList1Button3);
        nodesList1.addAnimatedNode(nodeList1Button4);
        nodesList1.setRotate(90);

        JFXNodesList nodesList = new JFXNodesList();
        nodesList.setSpacing(10);
        JFXButton nodeListButton = addButton("+","这里统一管理所有操作工具","");
        ImageView imageView = new ImageView(new Image(getClass().getResourceAsStream("/icon/加.png")));
        imageView.setFitWidth(30);
        imageView.setFitHeight(30);
        nodeListButton.setGraphic(imageView);
        nodesList.addAnimatedNode(nodeListButton);
        nodesList.addAnimatedNode(nodesList2);
        nodesList.addAnimatedNode(nodeList1Button5);
        nodesList.addAnimatedNode(nodesList3);
        nodesList.addAnimatedNode(nodesList1);
        nodesList.setRotate(180);

        return nodesList;
    }

    private JFXButton addButton(String name, String tip,String subButtonStyle){
        Label label = new Label(name);
        label.setStyle(FX_TEXT_FILL_WHITE);
        JFXButton jfxButton = new JFXButton();
        jfxButton.setGraphic(label);
        Tooltip tooltip = new Tooltip(tip);
        jfxButton.setTooltip(tooltip);
        jfxButton.setButtonType(JFXButton.ButtonType.RAISED);
        jfxButton.getStyleClass().add(ANIMATED_OPTION_BUTTON);
        if ("".equals(subButtonStyle)) {
            jfxButton.setStyle(FX_BACKGROUND_COLOR+";"+FX_TEXT_FILL_GREY);
        }else {
            jfxButton.getStyleClass().add(subButtonStyle);
        }
        return jfxButton;
    }

    public void reConstructNodesList(){
        if (anchorPane.getChildren().size()>1)
            anchorPane.getChildren().remove(1);
        JFXNodesList nodesList = constructPopup();
        nodesList.setLayoutX(950);
        nodesList.setLayoutY(0);
        anchorPane.getChildren().add(nodesList);
    }
}
